package com.example.controller;

import com.apifamily.model.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/name")
public class NameController {

    @GetMapping("/get")
    public String getNameGet(String name){
        return "GET 你的名字是:"+ name;
    }

    @PostMapping("post")
    public String getNamePost(@RequestParam String name){
        return "POST 你的名字是:"+ name;
    }

    @PostMapping("/user")
    public String getUserNamePost(@RequestBody User user, HttpServletRequest request){
/*        String accessKey = request.getHeader("accessKey");
        String nonce = request.getHeader("nonce");
        String timestamp = request.getHeader("timestamp");
        String sign = request.getHeader("sign");
        String body = request.getHeader("body");
        //TODO 实际情况是去数据库中查找
        if (!"admin".equals(accessKey)){
            throw new RuntimeException("无权限");
        }
        if (Long.parseLong(nonce) > 10000){
            throw new RuntimeException("无权限");
        }
        //TODO 时间和当前时间不能超过五分钟

        //TODO 实际情况从数据库中查出secretKey
        String serverSign = SignUtil.getSign(body, "admin123");
        if (!sign.equals(serverSign)){
            throw new RuntimeException("无权限");
        }*/

        String result = "POST 用户名字是" + user.getUsername();
        //TODO 调用成功后次数+1
        return result;
    }
}
